package descentFly.away3D4
{
	import UIFree.ObjectEvent;
	
	import away3d.containers.ObjectContainer3D;
	import away3d.core.math.Matrix3DUtils;
	import away3d.entities.Mesh;
	import away3d.tools.utils.Bounds;
	import away3d.vf.boxes;
	
	import descentFly.df_spaceObject;
	
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	
	import jiglib.math.JMatrix3D;

	public class df_points extends df_spaceObject {
		public static const TYPE_START		: int = 1;
		public static const TYPE_MONSTERGEN	: int = 2;
		public static const TYPE_PATH		: int = 3;
		public static const TYPE_DOOR		: int = 4;
		public static const TYPE_ENV		: int = 6;
		public static const TYPE_WND		: int = 7;
		
		private var _startIndex: Object;
		private var _items: Array;
		
		public function df_points(...parameters) {
			super();
			_startIndex = new Object();
			_items = new Array();
		}
		
		public static function getObjectID(obj: ObjectContainer3D): Object {
			var arr: Array = obj.originalName.split('.');
			return (arr.length>1)?arr[arr.length - 1]:0;
		}
		
		public function clear(): void {
			_items.forEach(function(item: Object, i: int, arr: Array): void {
				if (item.hasOwnProperty('mesh')) item.mesh.dispose(true);
			});
			_items.splice(0, _items.length);
		}
		
		public function parse(mesh: Mesh): void {
			var methodName: String = 'parse' + mesh.originalName.substr(0, 6).toLowerCase();
			if (hasOwnProperty(methodName)) 
				this[methodName](mesh);
			dispatchEvent(new ObjectEvent(methodName.toUpperCase(), mesh));
		}
		
		protected static function getOrientation(mesh: Mesh): Matrix3D {
			var mat: Matrix3D = mesh.transform.clone();
			mat.appendTranslation(-mesh.x, -mesh.y, -mesh.z);
			mat.appendScale(1/mesh.scaleX, 1/mesh.scaleY, 1/mesh.scaleZ);
			return mat;
		}
		
		public function parse_start(mesh: Mesh): void {
			_items.push({
				type		: TYPE_START,
				position	: mesh.position,
				orientation : getOrientation(mesh)
			})
			mesh.parent.removeChild(mesh);
		}

/*		public function parse_monst(mesh: Mesh): void {
			_items.push({
				type		: TYPE_MONSTERGEN,
				position	: mesh.position,
				orientation : getOrientation(mesh)
			})
			mesh.parent.removeChild(mesh);
		}*/
		
		public function parse_mpath(mesh: Mesh): void {
			_items.push({
				id			: getObjectID(mesh),
				type		: TYPE_PATH,
				path		: new df_pathFromMesh(mesh)
			})
			mesh.parent.removeChild(mesh);
		}
		
		public function parse_mdoor(mesh: Mesh): void {
			Bounds.getMeshBounds(mesh);
			_items.push({
				id			: getObjectID(mesh),
				type		: TYPE_DOOR,
				position	: mesh.position,
				orientation : getOrientation(mesh),
				box			: new Vector3D(Bounds.width, Bounds.height, Bounds.depth)
			})
			mesh.parent.removeChild(mesh);
		}
		
		public function parse_envir(a_mesh: Mesh): void {
			_items.push({
				id			: getObjectID(a_mesh),
				type		: TYPE_ENV,
				position	: a_mesh.position,
				orientation : getOrientation(a_mesh),
				mesh		: a_mesh
			})
		}
		
		public function parse_windw(a_mesh: Mesh): void {
			_items.push({
				id			: getObjectID(a_mesh),
				type		: TYPE_WND,
				position	: a_mesh.position,
				orientation : getOrientation(a_mesh),
				mesh		: a_mesh
			})
			a_mesh.parent.removeChild(a_mesh);
		}
		
		public function get items(): Array {
			return _items;
		}
		
		public function forEach(a_type: int, func: Function): Object {
			var i		: int = _startIndex[a_type];
			var index	: int = 0;
			while (i < _items.length) {
				if (_items[i].type == a_type) {
					var result: Object = func(_items[i], index);
					if (result) return result;
					index++;
				}
				i++;
			}
			return null;
		}
		
		public function getItemFromID(a_id: Object, retFunc: Function): void {
			var i: int = 0;
			while (i < _items.length) {
				if (_items[i].id == a_id) retFunc(_items[i]);
				i++;
			}
		}
		
		public function nextItemFromType(a_type: int): Object {
			if (_items.length == 0) return null
			if (!_startIndex.hasOwnProperty(a_type))
				_startIndex[a_type] = 0;
			var i: int = _startIndex[a_type];
			while (true) {
				i = (i + 1) % _items.length;
				if (_items[i].type == a_type) {
					_startIndex[a_type] = i;
					return _items[i];
				}
				if (i == _startIndex[a_type]) break;
			}
			return null;
		}
	}
}